import OfferAPIService from './_common/OfferAPIService';
import navigationSeevice from '../../../components/navigation/navigationSeevice';
import ToastUtil from '../../../utils/toast';
import { showMap, veridateMap, defualt, title, mandatory } from './_components/form/offerMap';
import Dictionary from '../../../utils/dictionary';
import Utils from '../../../utils/utils';

class OfferCreateMethods {

	/* 默认参数 */
	defualtParams = (props) => {
		const { salesType } = props.navigation.state.params || {};
		return {
			bizType: 'ADD',
			packageApplyInfo: {
				salesType,
				...defualt
			}
		}
	}

	/* 数据字典 */
	getDict = async (typeString) => {
		return await Dictionary.get(typeString) || [];
	}

	/* 报盘 保存/提交 */
	submit = async (params) => {
		console.log('报盘-提交数据', params);
		params.load = true;
		if (this.validation(params)) {
			params.load = false;
			return params;
		}
		let res = await OfferAPIService.offerAdd(params);
		params.load = false;
		if (res) {
			navigationSeevice.reset('OfferList', { salesType: params.packageApplyInfo.salesType }, [{ routeName: "HomePage" }]);
			//navigationSeevice.navigate('OfferList', { salesType: params.packageApplyInfo.salesType });
		}
		return params;
	}

	/* 商品回调数据 */
	goodsCallback = (data) => {
		//处理商品数据
		data && data.map((item, index) => {
			if (!Utils.isEmpty(item.udPrice)) {  // 升贴控制
				item.udPrice = parseInt(item.udPrice)
				if (!item.symbol) {
					item.udPrice = item.udPrice > 0 ? -item.udPrice : item.udPrice;
				} else {
					item.udPrice = Math.abs(item.udPrice);
				}
				//item.udPrice = item.symbol ? item.udPrice : parseInt(item.udPrice)
			}
			return item;
		})
		console.log('商品改变之后', data)
		return data;
	}


	/* 创建验证 */
	validation = (ceateData) => {  // 
		let { packageApplyInfo, saveType } = ceateData || {};
		let { isUnite, bizModel, areaId, resourceApplyList, isMustTran, isCustIntentionPlan, effectiveTimeType,
			upTime, salesType, intentionPlanBeginDate, intentionPlanEndDate, isSubmitPrepay, prepay, effectiveHour,
			effectiveMinute, prepayRate, payTypeArry, salesRuleId, channel } = packageApplyInfo || {};
		let result = false;


		//业务类型
		if (veridateMap.mdtrIsUniteFn(isUnite)) {
			ToastUtil.showWarning('请选择业务类型', 3000);
			return true;
		}
		//报盘类型
		if (veridateMap.mdtrIsUniteFn(bizModel)) {
			ToastUtil.showWarning('请选择报盘类型', 3000);
			return true;
		}
		//销售区域
		if (veridateMap.mdtrIsUniteFn(areaId)) {
			ToastUtil.showWarning('请选择销售区域', 3000);
			return true;
		}
		if (veridateMap.mdtrIsUniteFn(salesRuleId) && salesType === 'S') {
			ToastUtil.showWarning('请选择采销规则', 3000);
			return true;
		}
		if (veridateMap.mdtrIsUniteFn(channel) && salesType === 'S') {
			ToastUtil.showWarning('请选择定向客户', 3000);
			return true;
		}
		//商品
		if (!resourceApplyList) {
			ToastUtil.showWarning('请填写商品信息', 3000);
			return true;
		}
		if (Utils.isString(prepay) && prepay != '' && !Utils.isNumberComplex(prepay)) {
			ToastUtil.showWarning(`缴纳保证金金额不是有效数字`, 3000);
			result = true;
		}
		if (Utils.isString(effectiveHour) && effectiveHour != '' && !Utils.isNumberTime(effectiveHour)) {
			ToastUtil.showWarning(`资源有效时段小时不是有效数字`, 3000);
			result = true;
		}
		if (Utils.isString(effectiveMinute) && effectiveMinute != '' && !Utils.isNumberTime(effectiveMinute)) {
			ToastUtil.showWarning(`资源有效时段分钟不是有效数字`, 3000);
			result = true;
		}


		for (var i = 0; i < resourceApplyList.length; i++) {
			let { goodsName, maxCanSellNum, minBaseValue, maxBaseValue, price,
				udPrice, minOfferQuantity, offerMultiple, custPurNum, stepPrice, offerBottomPrice } = resourceApplyList[i] || {};
			if (veridateMap.mdtrIsUniteFn(goodsName)) { //计划采购量 计划销量
				ToastUtil.showWarning(`请选择第${i + 1}个商品`, 3000);
				result = true;
				break;
			}
			//特殊字符串控制 
			if (Utils.isString(maxCanSellNum) && maxCanSellNum != '' && !Utils.isNumberComplex(maxCanSellNum)) {
				ToastUtil.showWarning(`第${i + 1}个商品的${title.plan[salesType]}不是有效数字`, 3000);
				result = true;
			}
			if (showMap.isBaseValueFn(bizModel) && Utils.isString(minBaseValue) && minBaseValue != '' && !Utils.isNumberComplex(minBaseValue)) {
				ToastUtil.showWarning(`第${i + 1}个商品的成交量范围最小量不是有效数字`, 3000);
				result = true;
			}
			if (showMap.isBaseValueFn(bizModel) && Utils.isString(maxBaseValue) && maxBaseValue != '' && !Utils.isNumberComplex(maxBaseValue)) {
				ToastUtil.showWarning(`第${i + 1}个商品的成交量范围最大量不是有效数字`, 3000);
				result = true;
			}
			if (Utils.isString(price) && price != '' && !Utils.isNumberComplex(price)) {
				if (showMap.isPriceFn(bizModel)) {
					ToastUtil.showWarning(`第${i + 1}个商品的含税单价不是有效数字`, 3000);
					result = true;
				}
			}
			if (Utils.isString(offerBottomPrice) && offerBottomPrice != '' && !Utils.isNumberComplex(offerBottomPrice)) {
				if (mandatory.isOfferBottomPriceFn(bizModel)) {
					ToastUtil.showWarning(`第${i + 1}个商品的报盘底价不是有效数字`, 3000);
					result = true;
				}
			}
			if (Utils.isString(udPrice) && udPrice != '' && !Utils.isNumberComplex(udPrice)) {
				ToastUtil.showWarning(`第${i + 1}个商品的升贴水不是有效数字`, 3000);
				result = true;
			}
			if (Utils.isString(minOfferQuantity) && minOfferQuantity != '' && !Utils.isNumberComplex(minOfferQuantity)) {
				ToastUtil.showWarning(`第${i + 1}个商品的最小报盘数量不是有效数字`, 3000);
				result = true;
			}
			if (Utils.isString(offerMultiple) && offerMultiple != '' && !Utils.isNumberComplex(offerMultiple)) {
				ToastUtil.showWarning(`第${i + 1}个商品的报盘倍量不是有效数字`, 3000);
				result = true;
			}
			if (Utils.isString(custPurNum) && custPurNum != '' && !Utils.isNumberComplex(custPurNum)) {
				ToastUtil.showWarning(`第${i + 1}个商品的${title.number[salesType]}不是有效数字`, 3000);
				result = true;
			}
			if (Utils.isString(stepPrice) && stepPrice != '' && !Utils.isNumberComplex(stepPrice)) {
				ToastUtil.showWarning(`第${i + 1}个商品的客户提报含税单价加价梯度不是有效数字`, 3000);
				result = true;
			}
			//如果输入了报盘倍量,必须有最小报盘数量,因为倍量是在“最小报盘数量”基础上来了的
			if (!Utils.isEmpty(offerMultiple) && Utils.isEmpty(minOfferQuantity)) {
				ToastUtil.showWarning(`第${i + 1}个商品有设置倍量，最小起报值为必输`, 3000);
				result = true;
			}
		}
		if (saveType === "SUBMIT") {
			//定金比例 
			if (payTypeArry && payTypeArry.indexOf('2') != -1 && !Utils.isNumber(prepayRate)) {
				ToastUtil.showWarning('请输入保证金比例', 3000);
				return true;
			} else if (parseFloat(prepayRate) <= 0) {
				ToastUtil.showWarning('保证金比例必须大于0', 3000);
				return true;
			}
			//资源有效时段
			if (effectiveTimeType === 'O' && veridateMap.mdtrIsUniteFn(effectiveHour) && veridateMap.mdtrIsUniteFn(effectiveMinute)) {
				ToastUtil.showWarning(`请输入资源有效时段`, 3000);
				result = true;
			}
			//缴纳保证金
			if (isSubmitPrepay === 'Y' && veridateMap.mdtrIsUniteFn(prepay)) {
				ToastUtil.showWarning(`请输入缴纳保证金金额`, 3000);
				result = true;
			}
			//是否所有价格
			if (showMap.isFullPricesRequiredFn(bizModel) && veridateMap.mdtrIsUniteFn(isMustTran)) {
				ToastUtil.showWarning('请选择是否所有价格', 3000);
				return true;
			}
			//是否需要转成交报告
			if (veridateMap.mdtrIsUniteFn(isMustTran)) {
				ToastUtil.showWarning('请选择是否需要转成交报告', 3000);
				return true;
			}
			//是否客户按日提报 / 是否供应商按日提报
			if (veridateMap.mdtrIsUniteFn(isCustIntentionPlan)) {
				ToastUtil.showWarning(`请选择${title.submission[salesType]}`, 3000);
				return true;
			} else if (isCustIntentionPlan === 'Y' && (!intentionPlanBeginDate || !intentionPlanEndDate)) {
				ToastUtil.showWarning(`请填写${title.submission[salesType]}时间段`, 3000);
				return true;
			}
			//资源有效时长
			if (veridateMap.mdtrIsUniteFn(effectiveTimeType)) {
				ToastUtil.showWarning('请选择资源有效时长', 3000);
				return true;
			}
			//计划上架时间
			if (veridateMap.mdtrIsUniteFn(upTime)) {
				ToastUtil.showWarning('请选择计划上架时间', 3000);
				return true;
			}

			//商品子项 - 计划量
			for (var i = 0; i < resourceApplyList.length; i++) {
				let { maxCanSellNum, minBaseValue, maxBaseValue, price,
					offerBottomPrice, priceType, priceFormlaId, udPrice } = resourceApplyList[i] || {};

				if (veridateMap.mdtrIsUniteFn(maxCanSellNum)) { //计划采购量 计划销量
					ToastUtil.showWarning(`请填写第${i + 1}个商品的${title.plan[salesType]}`, 3000);
					result = true;
					break;
				}
				if (showMap.isBaseValueFn(bizModel) && (veridateMap.mdtrIsUniteFn(minBaseValue) || veridateMap.mdtrIsUniteFn(maxBaseValue))) { //成交量范围
					ToastUtil.showWarning(`请填写第${i + 1}个商品的成交量范围`, 3000);
					result = true;
					break;
				} else if (showMap.isBaseValueFn(bizModel) && parseInt(minBaseValue) > parseInt(maxBaseValue)) {
					ToastUtil.showWarning(`请正确填写第${i + 1}个商品的交量范围`, 3000);
					result = true;
					break;
				}
				if (veridateMap.mdtrIsUniteFn(priceType)) { //价格类型
					ToastUtil.showWarning(`请选择第${i + 1}个商品的价格类型`, 3000);
					result = true;
					break;
				} else if (priceType == '1') {//一口价
					if (showMap.isPriceFn(bizModel) && veridateMap.mdtrIsUniteFn(price)) { //含税单价
						ToastUtil.showWarning(`请选择第${i + 1}个商品的含税单价`, 3000);
						result = true;
						break;
					}
					if (mandatory.isOfferBottomPriceFn(bizModel) && veridateMap.mdtrIsUniteFn(offerBottomPrice)) { //报盘底价
						ToastUtil.showWarning(`请填写第${i + 1}个商品的报盘底价`, 3000);
						result = true;
						break;
					}
				} else if (priceType == '2') {//暂定价
					if (veridateMap.mdtrIsUniteFn(priceFormlaId)) { //结算公式
						ToastUtil.showWarning(`请选择第${i + 1}个商品的结算公式`, 3000);
						result = true;
						break;
					}
					if (veridateMap.mdtrIsUniteFn(udPrice)) { //升贴水
						ToastUtil.showWarning(`请填写第${i + 1}个商品的升贴水`, 3000);
						result = true;
						break;
					}
					// if (veridateMap.mdtrIsUniteFn(price)) { //暂定价
					// 	ToastUtil.showWarning(`请填写第${i + 1}个商品的暂定价`, 3000);
					// 	result = true;
					// 	break;
					// }
				}
			}
		}
		return result;
	}

	/* 是否所有价格必填”和“是否整报提报”，单商品时隐藏，多商品时触发展示让业务填写 (单商品隐藏时,落值为:是(Y)) */
	zhaoPriceSubmit = (sendData) => {
		let { packageApplyInfo } = sendData || {};
		let { bizModel, resourceApplyList } = packageApplyInfo || {};
		let isRefresh = false;
		if (bizModel === 'Z' || bizModel === 'DD') {
			if (resourceApplyList && resourceApplyList.length < 2) {
				sendData.packageApplyInfo.isFullPricesRequired = "Y";
				sendData.packageApplyInfo.isFullPackageFix = "Y";
			}
			isRefresh = true;
		}
		return { sendData, isRefresh };
	}






}

module.exports = new OfferCreateMethods();